using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice_Windows.Mapping;
using SolanumSolstice_Windows.Buildings;

namespace SolanumSolstice_Windows.Units
{
    public class Researcher : Human
    {

        protected bool isHealing;
        protected bool isResearching;
        protected ResearchBuilding targetBuilding;
        protected int healPower;
        protected float healingTime;
        protected int researchPower;
        protected float researchTime;

        #region Accessors

        public int HealPower { get { return healPower; } set { healPower = value; } }

        public int ResearchPower
        {
            get
            {
                return researchPower;
            }
            set
            {
                researchPower = value;
            }
        }

        public float ResearchTime
        {
            get
            {
                return researchTime;
            }
            set
            {
                researchTime = value;
            }
        }

        public bool IsResearching
        {
            get
            {
                return isResearching;
            }
            set
            {
                isResearching = value;
            }
        }
        #endregion

        public Researcher(ContentManager content, Vector2 position)
            : base(content, position)
        {
            texture = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\sci_researcher_sheet");
            hudTexture = content.Load<Texture2D>("Content\\Txtr\\Units\\HUD\\researcherHUD");
            this.hitPoints = 8;
            maxHitPoints = hitPoints;
            velocity = 2.0f;
            sightRadius = 2;
            attackPower = 1;
            unitClass = UnitClass.Science;

            isHealing = false;
            isResearching = false;
            healPower = 4;
            researchPower = 2;
            researchTime = 5;
            healingTime = 6;

            spawnCost = 8;
            timeCost = 8;

        }

        public override void Update(GameTime gameTime, List<Unit> enemiesInRange, List<Unit> friendsInRange)
        {

            //Check Recovery Time from actions
            TimeSpan elapsed = gameTime.ElapsedGameTime;

            recoveryTime -= elapsed.Milliseconds / 1000.0f;


            if (recoveryTime <= 0)
            {
                recoveryTime = 0;
                actionReady = true;
            }

            if (actionReady == true)
            {
                isHealing = false;
                isAttacking = false;
                int friendIndex = 0;



                for (int i = 0; i < friendsInRange.Count; i++)
                {


                    if (friendsInRange[i].HitPoints < friendsInRange[i].MaxHitPoints
                        || friendsInRange[i].IsAttacking == true)
                    {
                        isHealing = true;
                    }
                    if (friendsInRange[i].HitPoints < friendsInRange[i].MaxHitPoints)
                    {

                        if (friendsInRange[i].HitPoints < friendsInRange[friendIndex].HitPoints)
                        {
                            friendIndex = i;
                        }
                    }
                }

                if (isHealing == true && friendsInRange[friendIndex].HitPoints < friendsInRange[friendIndex].MaxHitPoints)
                {
                    friendsInRange[friendIndex].HealDamage(healPower);

                    actionReady = false;

                    recoveryTime = healingTime;
                    initialRecoveryTime = healingTime;
                }

                



                //Attack if enemy target is in range
                if (enemiesInRange.Count > 0 && isHealing == false)
                {
                    isAttacking = true;

                    if (actionReady == true)
                    {
                        int atkIndex = rand.Next(enemiesInRange.Count - 1);

                        this.DealDamage(enemiesInRange[atkIndex]);



                        recoveryTime = weapon.RecoveryTime;
                        initialRecoveryTime = weapon.RecoveryTime;

                        actionReady = false;

                    }

                }

                //Research
                if (isResearching == true && targetReached == true && inBuilding == false)
                {
                    targetBuilding.EnterBuildingAsResearcher(this);
                }
            }


            //Move if target is not reached
            if (!targetReached && isAttacking == false && isHealing == false)
            {
                position += velocity * direction;
            }

            //If Target Reached, Set Next Target or Stop
            if (Math.Abs(position.X - targetPosition.X) < velocity
                && Math.Abs(position.Y - targetPosition.Y) < velocity)
            {
                targetReached = true;

                if (pathIndex > 0 && path != null)
                {
                    pathIndex--;
                    targetPosition = path.GetPath[pathIndex].Position + groupOffset;
                    direction = targetPosition - position;
                    direction.Normalize();
                    targetReached = false;
                }
            }

        }

        public override void SetTarget(Vector2 target)
        {
            base.SetTarget(target);

            
            isHealing = false;
            isResearching = false;
            targetBuilding = null;
        }

        public void SetResearchingTarget(Vector2 target, Building targetBuild)
        {
            SetTarget(targetBuild.Entrance);
            isResearching = true;
            targetBuilding = (ResearchBuilding)targetBuild;

        }

        public override Object Clone()
        {
            Researcher u = new Researcher(this.content, this.position);

            u.weapon = this.weapon;
            u.hitPoints = this.hitPoints;
            u.maxHitPoints = this.maxHitPoints;
            u.recoveryTime = this.recoveryTime;
            u.initialRecoveryTime = this.initialRecoveryTime;
            u.position = this.position;
            u.velocity = this.velocity;
            u.sightRadius = this.sightRadius;
            u.attackRadius = this.attackRadius;
            u.attackPower = this.attackPower;
            u.isSelected = this.isSelected;
            u.isDead = this.isDead;
            u.isDying = this.isDying;
            u.isZombie = this.isZombie;
            u.inBuilding = this.inBuilding;
            u.isAttacking = this.isAttacking;
            u.actionSelected = this.actionSelected;
            u.actionReady = this.actionReady;
            u.rand = this.rand;
            u.actions = this.actions;
            u.currentActionIndex = this.currentActionIndex;
            u.targetReached = this.targetReached;
            u.direction = new Vector2(this.direction.X, this.direction.Y);
            u.unitClass = this.unitClass;

            u.isHealing=this.isHealing;
            u.isResearching=this.isResearching;
            u.targetBuilding=this.targetBuilding;
            u.healPower=this.healPower;
            u.healingTime=this.healingTime;
            u.researchPower=this.researchPower;
            u.researchTime=this.researchTime;

            u.timeCost = this.timeCost;
            u.spawnCost = this.spawnCost;

            return u;

        }


    }
}



